package com.gitee.cashzhang27.test.boot.activiti.config;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/04/12 11:34
 */
public class GenericResponseWrapper extends HttpServletResponseWrapper {
  private ByteArrayOutputStream output;
  private int contentLength;
  private String contentType;

  public GenericResponseWrapper(HttpServletResponse response) {
    super(response);
    this.output = new ByteArrayOutputStream();
  }

  public byte[] getData() {
    return this.output.toByteArray();
  }

  @Override
  public ServletOutputStream getOutputStream() {
    return new FilterServletOutputStream(this.output);
  }

  @Override
  public PrintWriter getWriter() {
    return new PrintWriter(this.getOutputStream(), true);
  }

  public int getContentLength() {
    return this.contentLength;
  }

  @Override
  public void setContentLength(int length) {
    this.contentLength = length;
    super.setContentLength(length);
  }

  @Override
  public String getContentType() {
    return this.contentType;
  }

  @Override
  public void setContentType(String type) {
    this.contentType = type;
    super.setContentType(type);
  }
}
